/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.model;

import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.xw.framework.base.dao.BaseUpdatableEntity;

/**
 * The Class SysMenu.
 *
 * @author Xi Wei
 */
@Entity
@Table(name="sys_menu")
@DynamicUpdate(value=true)
@DynamicInsert(value=true)
public class SysMenu extends BaseUpdatableEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3750136672739265881L;
	private String menuCode;
	private String menuName;
	private int menuLevel = 1;	
	private String menuUrl;
	private String menuIcon = "menu-icon fa fa-desktop";
	private int menuOrder = 1;
	private int menuType = 1;
	@ManyToOne
	@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
	@JoinColumn(name = "parent_id", nullable = true)
	@JsonIgnore
	private SysMenu parent;
	
	
	@ManyToOne
	@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
	@JoinColumn(name = "system_id", nullable = true)
	@JsonIgnore
	private SystemConfig system;
	
	@OneToMany(mappedBy="parent", cascade={javax.persistence.CascadeType.ALL}, fetch=FetchType.LAZY)
	@OrderBy("menuOrder")
	@JsonIgnore
	private List<SysMenu> subMenuList = Lists.newArrayList();
	
	

	
	/**
	 * Gets the system.
	 *
	 * @return the system
	 */
	public SystemConfig getSystem() {
		return system;
	}

	/**
	 * Sets the system.
	 *
	 * @param system the new system
	 */
	public void setSystem(SystemConfig system) {
		this.system = system;
	}

	/**
	 * Gets the parent.
	 *
	 * @return the parent
	 */
	public SysMenu getParent(){
		return parent;
	}
	
	/**
	 * Sets the parent.
	 *
	 * @param parent the new parent
	 */
	public void setParent(SysMenu parent) {
		this.parent = parent;
	}

	/**
	 * Gets the sub menu list.
	 *
	 * @return the sub menu list
	 */
	public List<SysMenu> getSubMenuList() {
		return subMenuList;
	}

	/**
	 * Sets the sub menu list.
	 *
	 * @param subMenuList the new sub menu list
	 */
	public void setSubMenuList(List<SysMenu> subMenuList) {
		this.subMenuList = subMenuList;
	}

	
	/**
	 * Gets the menu code.
	 *
	 * @return the menu code
	 */
	public String getMenuCode() {
		return menuCode;
	}

	/**
	 * Sets the menu code.
	 *
	 * @param menuCode the new menu code
	 */
	public void setMenuCode(String menuCode) {
		this.menuCode = menuCode;
	}

	/**
	 * Gets the menu name.
	 *
	 * @return the menu name
	 */
	public String getMenuName() {
		return menuName;
	}

	/**
	 * Sets the menu name.
	 *
	 * @param menuName the new menu name
	 */
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	
	
	/**
	 * Gets the menu level.
	 *
	 * @return the menu level
	 */
	public int getMenuLevel() {
		return menuLevel;
	}

	/**
	 * Sets the menu level.
	 *
	 * @param menuLevel the new menu level
	 */
	public void setMenuLevel(int menuLevel) {
		this.menuLevel = menuLevel;
	}

	/**
	 * Gets the menu icon.
	 *
	 * @return the menu icon
	 */
	public String getMenuIcon() {
		return menuIcon;
	}

	/**
	 * Sets the menu icon.
	 *
	 * @param menuIcon the new menu icon
	 */
	public void setMenuIcon(String menuIcon) {
		this.menuIcon = menuIcon;
	}

	/**
	 * Gets the menu url.
	 *
	 * @return the menu url
	 */
	public String getMenuUrl() {
		return menuUrl;
	}
	
	/**
	 * Sets the menu url.
	 *
	 * @param menuUrl the new menu url
	 */
	public void setMenuUrl(String menuUrl) {
		this.menuUrl = menuUrl;
	}
	
	/**
	 * Gets the menu order.
	 *
	 * @return the menu order
	 */
	public int getMenuOrder() {
		return menuOrder;
	}
	
	/**
	 * Sets the menu order.
	 *
	 * @param menuOrder the new menu order
	 */
	public void setMenuOrder(int menuOrder) {
		this.menuOrder = menuOrder;
	}
	
	/**
	 * Gets the menu type.
	 *
	 * @return the menu type
	 */
	public int getMenuType() {
		return menuType;
	}
	
	/**
	 * Sets the menu type.
	 *
	 * @param menuType the new menu type
	 */
	public void setMenuType(int menuType) {
		this.menuType = menuType;
	}
	
	/**
	 * Gets the parent id.
	 *
	 * @return the parent id
	 */
	@Transient
	public Long getParentId(){
		if(parent != null){
			return parent.getId();
		}
		return 0l;
	}
	
	/**
	 * Checks if is checks for sub menu.
	 *
	 * @return true, if is checks for sub menu
	 */
	@Transient
	public boolean isHasSubMenu(){
		if(subMenuList == null || subMenuList.isEmpty()){
			return false;
		}
		return true;
	}
	
	@JsonIgnore
	@ManyToMany(fetch = FetchType.LAZY)
	@Fetch(FetchMode.SUBSELECT)
	@Cascade(value=CascadeType.ALL)
	@JoinTable(name = "acct_role_menu", joinColumns = { @JoinColumn(name = "menu_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	private List<AcctRole> roleList = Lists.newArrayList();

	/**
	 * Gets the role list.
	 *
	 * @return the role list
	 */
	public List<AcctRole> getRoleList() {
		return roleList;
	}

	/**
	 * Sets the role list.
	 *
	 * @param roleList the new role list
	 */
	public void setRoleList(List<AcctRole> roleList) {
		this.roleList = roleList;
	}
	
}
